package singleton;

//
public class SingletonDemo2 {
    private static SingletonDemo2 instance = null;

    private SingletonDemo2 () {
        //构造器私有，不能通过new创建对象
        System.out.println(Thread.currentThread().getName() + "\t 我是构造方法SingletonDemo");
    }

    public synchronized static SingletonDemo2 getInstance() {
        if(instance == null) {
            instance = new SingletonDemo2();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            //创建10个线程，分别获取单列对象
            new Thread(()->{
                SingletonDemo2.getInstance();//只要是线程安全的，构造函数只打印一次
            },String.valueOf(i)).start();
        }

    }
}
